<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notify extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('notify_m');
	}
	
	/*--新发布通知公告视图------------------------------------------------------------------------------*/
	public function notify_add(){
		if($this->acl->check_privilege('notify_add') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$id = $this->uri->segment(3, 0);
			if($id != ''){
				$data['status'] = 1;
				$data['arr'] = $this->notify_m->contentbox($id);
			}else{
				$data['status'] = 0;
			}
			$this->load->view('notify/new',$data);
		}
	}

	/*--新发布通知公告POST提交处理------------------------------------------------------------------------------*/
	public function _notify_add_post(){
		if($this->acl->check_privilege('notify_add') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$arrdata=$this->input->post();
			$data = $this->notify_m->set_add_post($arrdata);
			$this->rejsonsuccess($data,"发送邮件失败，请联系管理员");	
		}
	}


	/*通知公告列表视图*/
	public function inbox(){
		if($this->acl->check_privilege('notify_inbox') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$this->load->view('notify/inbox');
		}
	}
	//通知公告列表JSON数据
	public function _inbox_data_post(){
		if($this->acl->check_privilege('notify_inbox') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$rowdata = '';
			$data = $this->notify_m->getinboxlistdata($this->input->post());
			if(!empty($data['data'])){
				foreach ($data['data'] as $v){
					$rowdata[] = array("id"=>$v['notify_id'],"dept_name"=>$v['dept_name'],"type_id"=>$v['type_id'],"title"=>$v['title'],"sender"=>$v['from_name'],"to_name"=>$v['to_name'],"sendertime"=>$v['sendertime'],'isattach'=>$v['attachment_id']);
				}
			}			
			$data = array("total"=>$data['total'],'rows'=>$rowdata);
			echo json_encode($data); 
		}
	}

	/*----发布列表视图------------*/
	public function sentbox(){
		if($this->acl->check_privilege('notify_sentbox') == false){
			show_error("you have no privilege to access this page");
			return ;
		}else{
			$this->load->view('notify/sentbox');
		}
	}
	//发件箱JSON数据
	public function _sentbox_data_post(){
		if($this->acl->check_privilege('notify_sentbox') == false){
			show_error("you have no privilege to access this page");
			return ;
		}else{
			$rowdata = '';
			$data = $this->notify_m->getsentboxlistdata($this->input->post());
			if(!empty($data['data'])){
				foreach ($data['data'] as $v){
					$rowdata[] = array("id"=>$v['notify_id'],"dept_name"=>$v['dept_name'],"type_id"=>$v['type_id'],"title"=>$v['title'],"from_name"=>$v['from_name'],"to_name"=>$v['to_name'],"sendertime"=>$v['sendertime'],'begin_date'=>$v['begin_date'],'end_date'=>$v['end_date'],'isattach'=>$v['attachment_id']);
				}
			}
			$data = array("total"=>$data['total'],'rows'=>$rowdata);
			echo json_encode($data); 
		}
	}
	
	/*---notify详细内容视图-----*/
	function contentbox(){
		$id = $this->uri->segment(3, 0);				
		$data['contentview'] = $this->notify_m->contentbox($id);
		if($data['contentview']){
			$data['attlist'] = $this->attlist('notify',$data['contentview'][0]->attachment_id,$data['contentview'][0]->attachment_name,$data['contentview'][0]->size,$data['contentview'][0]->notify_id);
			$this->load->view('notify/contentbox',$data);
		}else{
			echo "<script>var tab = $('#main-tab').tabs('getSelected');var index = $('#main-tab').tabs('getTabIndex',tab);$('#main-tab').tabs('close',index);$.messager.alert('错误','没有找到该条通知公告，可能已被删除！','error');</script>";
		}
	}
	/*---详细内容页面中的框架调用该方法，只返回CONTENT的值----*/
	function get_notify_content(){
		$id = $this->uri->segment(3, 0);
		$contentview = $this->notify_m->contentbox($id);
		echo $contentview[0]->content;
	}
	
	//设置为已读
	public function _notify_setread_post(){
		$id=$this->input->post();
		$data = $this->notify_m->set_read($id);
		$this->rejsonsuccess($data,"'标记为已读'失败，请联系管理员");		
	}
	
	//删除邮件
	public function _notify_del_post(){
		$data=$this->input->post();
		$data = $this->notify_m->notify_del($data);
		$this->rejsonsuccess($data,"'删除邮件'失败，请联系管理员");		
	}
	
	//高级搜索功能视图页
	function advanced_search(){
		$m = $this->uri->segment(3, 0);
		if($m == 'notify_in'){
			$t = '通知公告';
		}else if($m == 'notify_sent'){
			$t = '发布管理';	
		}
		$data['v'] = array('m'=>$m,'t'=>$t);
		$this->load->view('notify/notify_search',$data);
	}
}
?>